#include "CorelDbgHelp.h"

#ifndef USE_SYSTEM_DBGHELP_DLL

CCorelDbgHelp::PFN_SymFunctionTableAccess64 CCorelDbgHelp::g_pfnSymFunctionTableAccess64 = NULL;
CCorelDbgHelp::PFN_SymGetModuleBase64 CCorelDbgHelp::g_pfnSymGetModuleBase64 = NULL;

CCorelDbgHelp::CCorelDbgHelp()
: m_hModule(NULL),
  m_pfnSymCleanup(NULL),
  m_pfnSymSetOptions(NULL),
  m_pfnSymInitializeA(NULL),
  m_pfnSymInitializeW(NULL),
  m_pfnSymLoadModule64(NULL),
  m_pfnStackWalk64(NULL),
  m_pfnSymGetModuleInfo64(NULL),
  m_pfnSymFromAddr(NULL),
  m_pfnSymGetLineFromAddr(NULL)
{
    WCHAR t_wszDllPath[MAX_PATH];
    DWORD t_dwRet = GetEnvironmentVariableW(ENVSTR_SERVER_PATH, t_wszDllPath, MAX_PATH);

    if (t_dwRet && MAX_PATH >= t_dwRet)
    {
        wcscat_s(t_wszDllPath, MAX_PATH, L"\\dbghelp.dll");
        m_hModule = LoadLibraryW(t_wszDllPath);
        if (m_hModule)
        {
            m_pfnSymCleanup     = (PFN_SymCleanup)GetProcAddress(m_hModule, "SymCleanup");
            m_pfnSymSetOptions  = (PFN_SymSetOptions)GetProcAddress(m_hModule, "SymSetOptions");
            m_pfnSymInitializeA = (PFN_SymInitializeA)GetProcAddress(m_hModule, "SymInitialize");
            m_pfnSymInitializeW = (PFN_SymInitializeW)GetProcAddress(m_hModule, "SymInitializeW");
            m_pfnSymLoadModule64= (PFN_SymLoadModule64)GetProcAddress(m_hModule, "SymLoadModule64");
            m_pfnStackWalk64    = (PFN_StackWalk64)GetProcAddress(m_hModule, "StackWalk64");
            g_pfnSymFunctionTableAccess64 = (PFN_SymFunctionTableAccess64)GetProcAddress(m_hModule, "SymFunctionTableAccess64");
            g_pfnSymGetModuleBase64 = (PFN_SymGetModuleBase64)GetProcAddress(m_hModule, "SymGetModuleBase64");
            m_pfnSymGetModuleInfo64 = (PFN_SymGetModuleInfo64)GetProcAddress(m_hModule, "SymGetModuleInfo64");
            m_pfnSymFromAddr = (PFN_SymFromAddr)GetProcAddress(m_hModule, "SymFromAddr");
            m_pfnSymGetLineFromAddr = (PFN_SymGetLineFromAddr)GetProcAddress(m_hModule, "SymGetLineFromAddr");
        }
    }
}

CCorelDbgHelp::~CCorelDbgHelp()
{
    if (m_hModule)
    {
        FreeLibrary(m_hModule);
    }
}

#endif // USE_SYSTEM_DBGHELP_DLL